<?php
namespace app\admin\controller;

use app\model\admin;
use think\Request;
use think\Session;
use think\Controller;
use think\Config;

class Seo extends Controller
{
    //生成静态html页面
    public function rewrite_index(){
        return $this->fetch('seo/rewrite_index');
    }

    //生成静态html页面
    public function rewrite()
    {
        Config::load(APP_PATH.'other.php');
        $id = $request = $this->request->get('type');
        if($id>0){
            if($id==1){//首页
                $web_url['url'] = Config::get('web_url').'/index.php'; //网站地址
                $web_url['type'] = 1;
                $url[] = $web_url;
            }elseif($id==2){//栏目页
                $domain = Config::get('url');
                $c_dao = new \app\model\admin\Column();
                $s_dao = new \app\model\admin\System();
                $c_where['status']=1;
                $c_lists = $c_dao->get_column_lists($c_where,$sort='id desc',$paginate=10);
                $s_lists = $s_dao->navigation_list('',$sort='sort desc',$paginate=15);//查询前台网站菜单

                if($c_lists){
                    foreach($c_lists as $val){
                        $web_url['url'] = $domain.$val['seo_name_en'];
                        $web_url['type'] = 2;
                        $url[] = $web_url;
                    }
                    $url[] = $web_url;
                }
                if($s_lists){
                    foreach($s_lists as $cal){
                        $web_url['url'] = $cal['url'];
                        $web_url['type'] = 2;
                        $url[] = $web_url;
                    }
                    $url[] = $web_url;
                }
                $url = two_array_unique($url,'url');//去除重复的url
                $all_url=[];
                foreach($url as $a_url){
                    if($a_url['url'] == Config::get('url')){
                        unset($a_url);
                    }else{
                        $all_url[] = $a_url;
                    }
                }
                unset($url);
                $url = $all_url;//重新给url变量赋值

            }elseif($id==3){//文章页
                $domain = Config::get('url');
                $a_dao = new \app\model\front\Article();
                $where['status']='1';
                $a_lists = $a_dao->get_article_all($where,$sort='id desc');//查询所有文章
//                $this->assign('a_lists', $a_lists);
                if($a_lists){
                    foreach($a_lists as $val){
                        $article_url['url'] = $domain.$val['column_name'].'/'.$val['seo_name_en'].'.html';
                        $article_url['type'] = 3;
                        $url[] = $article_url;
                    }
                }
            }
            $array_url = $url;
            $dao = new \app\model\Seo();
            $err_list=[];
            $err_list_two=[];
            $err_list_three=[];
            $err_list_four=[];
            $err_list_five=[];
            $err_list_six=[];
            foreach ($array_url as $url){
                $status = $dao->rewrite($url);
                if($status['status']==9999){
                    $err_list[] = $status['error_info'];
                }
            }
            if(!empty($err_list)){//如果存在生成失败的
                foreach ($err_list as $url){
                    $turl['type']=2;
                    $turl['url']=$url;
                    $status = $dao->rewrite($turl);
                    if($status['status']==9999){
                        $err_list_two[] = $status['error_info'];
                    }
                }
                if(!empty($err_list_two)){
                    foreach ($err_list_two as $url){
                        $turl['type']=2;
                        $turl['url']=$url;
                        $status = $dao->rewrite($turl);
                        if($status['status']==9999){
                            $err_list_three[] = $status['error_info'];
                        }
                    }
                }
                if(!empty($err_list_three)){
                    foreach ($err_list_three as $url){
                        $turl['type']=2;
                        $turl['url']=$url;
                        $status = $dao->rewrite($turl);
                        if($status['status']==9999){
                            $err_list_four[] = $status['error_info'];
                        }
                    }
                }
                if(!empty($err_list_four)){
                    foreach ($err_list_four as $url){
                        $turl['type']=2;
                        $turl['url']=$url;
                        $status = $dao->rewrite($turl);
                        if($status['status']==9999){
                            $err_list_five[] = $status['error_info'];
                        }
                    }
                }
                if(!empty($err_list_five)){
                    foreach ($err_list_five as $url){
                        $turl['type']=2;
                        $turl['url']=$url;
                        $status = $dao->rewrite($turl);
                        if($status['status']==9999){
                            $err_list_six[] = $status['error_info'];
                        }
                    }
                }
                $data['status']=9999;
                $data['data']=$err_list_six;
                $data['mag']='生成失败';
            }else{
                $data['status']=0;
                $data['data']=(object)[];
                $data['mag']='ok';
            }
            return json($data);
        }
    }

    //生成短域名页面
    public function shot_url(){
        //栏目页面
        Config::load(APP_PATH.'other.php');
        $domain = Config::get('url');
        $c_dao = new \app\model\admin\Column();
        $s_dao = new \app\model\admin\System();
        $c_where['status']=1;
        $c_lists = $c_dao->get_column_lists($c_where,$sort='id desc',$paginate=10);
        $s_lists = $s_dao->navigation_list('',$sort='sort desc',$paginate=15);//查询前台网站菜单
        if($c_lists){
            foreach($c_lists as $val){
                $web_url['url'] = $domain.$val['seo_name_en'];
                $c_url[] = $web_url;
            }
        }
        if($s_lists){
            foreach($s_lists as $cal){
                $web_url['url'] = $cal['url'];
                $s_url[] = $web_url;
            }
        }

        //文章页面
        $a_dao = new \app\model\front\Article();
        $where['status']='1';
        $a_lists = $a_dao->get_article_all($where,$sort='id desc');//查询所有文章
        $this->assign('a_lists', $a_lists);
        if($a_lists){
            foreach($a_lists as $val){
                $article_url['url'] = $domain.$val['column_name'].'/'.$val['seo_name_en'].'.html';
                $a_url[] = $article_url;
            }
        }

        $url = array_merge($c_url,$s_url,$a_url);
        $url = two_array_unique($url,'url');//去除重复的url
        $this->assign('url', $url);
        return $this->fetch('seo/surl_index');
    }

    //生成短域名
    public function get_short_url(){
        $url = $request = $this->request->post('all_url');
        $url_array = explode("=lh",$url);

        foreach($url_array as $val){
            if (preg_match("/\s/", $val)) {
                unset($val);
            }else{
                $url_980 = 'http://980.so/api.php?url='.urlencode($val);
                $url_uee = 'http://api.uee.me/api.php?url='.urlencode($val);
                $info_url[] = http_curl($url_980);
                $info_url[] = http_curl($url_uee);
//                $url = 'http://suo.im/api.php?url='.urlencode($val);
//                $info_url[] = addslashes(http_curl($url));
            }
        }
        $data['status']=0;
        $data['data']=$info_url;
        $data['msg']='ok!';
        return json($data);
    }

    //生成短域名
    public function tuiguang_url(){
        $url = $request = $this->request->post('short_url');
        $url_array = explode("#",$url);
        $info_url=[];
        foreach($url_array as $val){
            if (preg_match("/\s/", $val)) {
                unset($val);
            }else{
                $info_url[] = $val;
            }
        }

        $url_array_urm = count($info_url);
        $t_url_num = Session::get('t_url');
        if(!empty($t_url_num)){
            if($t_url_num==$url_array_urm) {
                $t_url_num = 1;
                Session::set('t_url',1);
            }
            $t_url_num++;;
            Session::set('t_url',$t_url_num);
        }else{
            $t_url_num = 1;
            Session::set('t_url',1);
        }
        var_dump($info_url[$t_url_num]);
        //推广访问的地址
        $open_url = array('http://www.aizhan.com/cha/');
        $now_url = $open_url[0].urldecode($info_url[$t_url_num]);
//        return json($open_url[0]);
        var_dump($now_url);die();
        http_curl($now_url);


        return json($info_url);
    }

    //获取网站关键词排名
    public function baidu_keyword_ranking(){
        $dao = new \app\model\admin\System();
        Config::load(APP_PATH.'other.php');
        $web_url = Config::get('web_url');
        $data = $dao->web_info();//查询网站信息
        $keywords = $data['web_keywords'];
        $keywords=explode(',',$keywords);
        foreach($keywords as $val){
            $web_rank['keyword'] = $val;
            $rank = $dao->baidu_keyword_ranking($val,0);
            $rank = object2array(json_decode($rank));
            $rank = object2array($rank['feed']);
            $rank =$rank['entry'];
            foreach ($rank as $key => $item) {
                if($key==50)break;
                $status = strpos($item->url,$web_url);
                if(!$status){
                    $web_rank['rank'] = '50+';
                }else{
                    $web_rank['rank'] = $item->pn;
                    break;
                }
            }
            $web_keywords_rank[] = $web_rank;
        }
        return json(['status'=>0,'data'=>$web_keywords_rank,'msg'=>'ok!']);
    }

    //百度事实推送
    public function baidu_tuisong(){
        $dao = new \app\model\admin\System();
        if (Request::instance()->isPOST()){
            $url = deletehtml($request = $this->request->post('url'));
            $type = deletehtml($request = $this->request->post('type'));
            if(empty($url))return json(['status'=>1000,'data'=>(object)[],'msg'=>'链接不能为空!']);
            $data = $dao->web_info();//查询网站信息
            if(empty($data['baidu_token']))return json(['status'=>1001,'data'=>(object)[],'msg'=>'百度token为空!']);

            if($type){
                $url = explode(',',$url);
            }else{
                $url = array($url);
            }
            $status = json_decode(baidu_tuisong($data['baidu_token'],$url));
            if(isset($status->success)){
                return json(['status'=>0,'data'=>(object)[],'msg'=>'推送成功!']);
            }else{
                return json(['status'=>1003,'data'=>(object)[],'msg'=>$status->message]);
            }
        }
    }

    //查询网站收录量
    public function baidu_web_count(){
        $dao = new \app\model\admin\System();
        Config::load(APP_PATH.'other.php');
        $web_url = Config::get('web_url');
        $get_web_count = $dao->baidu_web_count('site:'.$web_url);

        $count = '0';
        if($get_web_count){
            $get_web_count = object2array(json_decode($get_web_count));
            $get_web_count = object2array($get_web_count['feed']);
            $count = $get_web_count['all'];
        }
        return json(['status'=>0,'data'=>$count,'msg'=>'ok']);
    }

    //蜘蛛列表
    public function spider_lists()
    {
        $dao = new \app\model\admin\Charts();
        $start_time = $request = $this->request->get('start_time');
        $end_time = $request = $this->request->get('end_time');
        if(empty($start_time)){
            $start_time = strtotime(date("Y-m-d"));
        }else{
            $start_time = strtotime($start_time);
        }
        if(empty($end_time)){
            $end_time = strtotime(date("Y-m-d 24:00:00"));
        }else{
            $end_time = strtotime($end_time);
        }

        $between['one'] = $start_time; //开始值
        $between['two'] = $end_time;   //结束值
        $between['val'] = 'time';  //条件值
        $lists = $dao->get_spider_lists_between($between,$sort='id desc',$paginate=100);
        $this->assign('lists', $lists);
        $page = $lists->render();
        $this->assign('page', $page);
//        return json($lists);
        return $this->fetch('seo/spider_lists');
    }
    //蜘蛛列表
    public function spider_index()
    {
        return $this->fetch('seo/spider_chart');
    }

    public function spider_charts(){
        $type = $request = $this->request->get('type');
        if($type=='pie'){
            $dao = new \app\model\admin\Charts();
            $start_time = $request = $this->request->get('start_time');
            $end_time = $request = $this->request->get('end_time');
            if(empty($start_time)){
                $start_time = strtotime(date("Y-m-d"));
            }else{
                $start_time = strtotime($start_time);
            }
            if(empty($end_time)){
                $end_time = strtotime(date("Y-m-d 24:00:00"));
            }else{
                $end_time = strtotime($end_time);
            }

            $between['one'] = $start_time; //开始值
            $between['two'] = $end_time;   //结束值
            $between['val'] = 'time';  //条件值
            $result = $dao->spider_charts_pie($between,$where='');
            if(empty($result))return json(['status'=>0,'data'=>[],'msg'=>'暂无数据!']);
            foreach($result as $val){
                $name[] = $val['spider'];
                $value[]  = array('name'=>$val['spider'],'value'=>$val['num'],);
            }
            $data['spider_name'] = $name;
            $data['value'] = $value;
            return json(['status'=>0,'data'=>$data,'msg'=>'操作成功!']);
        }elseif($type=='line'){
            $dao = new \app\model\admin\Charts();
            $start_time = $request = $this->request->get('start_time');
            $end_time = $request = $this->request->get('end_time');
            $spider_type_one = $request = $this->request->get('spider');
            if(empty($start_time)){
                $start_time = strtotime(date("Y-m-d"));
            }else{
                $start_time = strtotime($start_time);
            }
            if(empty($end_time)){
                $end_time = strtotime(date("Y-m-d 24:00:00"));
            }else{
                $end_time = strtotime($end_time);
            }

            $between['one'] = $start_time; //开始值
            $between['two'] = $end_time;   //结束值
            $between['val'] = 'time';  //条件值
            $result = $dao->spider_charts_line($between,$where='');

            //获取两个日期之间的所有日期
            $dates = prDates($start_time,$end_time);

            foreach($dates as $f_datas){
                $all_dates[] = trim($f_datas);
            }

            //根据时间戳获取当天日期。获取一共多少个蜘蛛种类
            foreach($result as $val){
                $spider[] = $val['spider'];
            }

            $spider = array_unique($spider);
            $spider = array_values($spider);//蜘蛛种类
            if(empty($spider_type_one)){
                return json(['status'=>0,'data'=>$spider,'msg'=>'操作成功!']);
            }else{
                $where_one['spider'] = $spider_type_one;
                $result_one = $dao->spider_charts_line($between,$where_one);
                foreach($result_one as $val_zl){
                    $ndate[] = $val_zl['date'] = date('Y-m-d',$val_zl['time']);
                    $this_spider[] = $val_zl;
                }
                $result_one = $dao->spider_charts_line($between,$where_one);
                foreach($result_one as $val_zl){
                    $ndate[] = $val_zl['date'] = date('Y-m-d',$val_zl['time']);

                    $this_spider[] = $val_zl;
                }

                $result_20170918 = array();
                foreach ($this_spider as $data) {
                    isset($result[$data['date']]) || $result[$data['time']] = array();
                    $result_20170918[$data['date']][] = $data['spider'];
                    $a_date[] = $data['date'];
                }

                foreach($result_20170918 as $key=>$end_result){
                    $num = count($end_result);
                    $data_now['date'][] = $key;
                    $data_now['value'][] = $num;
                }
            }
            return json(['status'=>0,'data'=>$data_now,'msg'=>'操作成功!']);
        }

    }

}


